package top.hkyzf.study.leetcode.array;

import org.junit.Test;

/**
 * @author 朱峰
 * @date 2022-1-19 11:47
 */
public class 搜索插入位置_35 {
    public int searchInsert(int[] nums, int target) {
        int start = 0;
        int end = nums.length - 1;
        while (start <= end) {
            int middle = (end - start) / 2 + start;
            if(target > nums[middle]) {
                start = middle + 1;
            } else if(target < nums[middle]) {
                end = middle -1;
            } else {
                return middle;
            }
        }
        return start;
    }

    @Test
    public void testSearchInsert () {
        int [] nums = {1,3,5,7};
        int target = 6;
        System.out.println(searchInsert(nums, target));
    }
}
